﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Seeker.DAL.Interface;
using Seeker.Model;
using Seeker.Model.Interface;

namespace Seeker.DAL.Admin
{
    public class AUserInfo : IGetAUserInfo
    {
        /// <summary>
        /// 获取单个管理员信息
        /// </summary>
        /// <param name="uid">管理员id</param>
        /// <returns>管理员基本信息 IBaseAUserFullInfo</returns>
        public IBaseAUserFullInfo GetInfo(int uid)
        {
            IBaseAUserFullInfo userInfo = new AFullUser();
            string commandText = @"SELECT A.UID, AGL.GID, ACCOUNT, ACCOUNT_PWD, REAL_NAME, STATUS, TEL, QQ, EMAIL, FACE_URL 
                                       FROM AUSER AS A INNER JOIN AUSER_GROUPS_LIST AS AGL ON A.UID = AGL.UID
                                   WHERE A.UID = @UID";
            SqlParameter[] parameters = { new SqlParameter("@UID", SqlDbType.Int) };

            parameters[0].Value = uid;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AdminConnectionStr, CommandType.Text, commandText, parameters);

            if (dr.Read())
            {
                userInfo.UID = (int)dr["UID"];
                userInfo.Account = dr["ACCOUNT"].ToString();
                userInfo.GID = (int)dr["GID"];
                userInfo.Password = dr["ACCOUNT_PWD"].ToString();
                userInfo.RealName = dr["REAL_NAME"].ToString();
                userInfo.Status = (bool)dr["STATUS"];
                userInfo.Telephone = dr["TEL"].ToString();
                userInfo.QQ = dr["QQ"].ToString();
                userInfo.Email = dr["EMAIL"].ToString();
                userInfo.FaceUrl = dr["FACE_URL"].ToString();

                return userInfo;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取多个管理员信息
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pageSize">每页含有信息个数</param>
        /// <param name="where">筛选条件</param>
        /// <param name="orderBy">排序</param>
        /// <returns>多个管理员信息</returns>
        public List<AUsers> GetInfoList(int current, int pageSize, string where, string orderBy)
        {
            List<AUsers> au = new List<AUsers>();
            List<SqlParameter> parameters = new List<SqlParameter>();

            string commandText = "SP_PAGINATION";
            parameters.Add(new SqlParameter("@Tables", "AUSER"));
            parameters.Add(new SqlParameter("@PageSize", pageSize));
            parameters.Add(new SqlParameter("@CurrentPage", current));
            parameters.Add(new SqlParameter("@Filter", where == null ? "" : where));
            parameters.Add(new SqlParameter("@Sort", orderBy == null ? "" : orderBy));
            parameters.Add(new SqlParameter("@PrimaryKey", "UID"));
            parameters.Add(new SqlParameter("@Fields", "UID, ACCOUNT, REAL_NAME, STATUS, TEL, QQ, EMAIL"));

            try
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AdminConnectionStr, CommandType.StoredProcedure, commandText,
                    parameters.ToArray());

                while (dr.Read())
                {
                    au.Add(new AUsers()
                    {
                        UID = (int)dr["UID"],
                        Account = dr["ACCOUNT"].ToString(),
                        RealName = dr["REAL_NAME"].ToString(),
                        Email = dr["EMAIL"].ToString(),
                        QQ = dr["QQ"].ToString(),
                        Telephone = dr["TEL"].ToString(),
                        Status = (bool)dr["STATUS"]
                    });
                }

                return au;

            }
            catch(SqlException se)
            {
               
                #if debug
                 throw new Exception(se.Message);
                #endif

                return null;

            }
        }


        /// <summary>
        /// 获取多个管理员信息
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pageSize">每页含有信息个数</param>
        /// <param name="where">筛选条件</param>
        /// <returns>多个管理员信息</returns>
        public List<Model.AUsers> GetInfoList(int current, int pageSize, string where)
        {
            return GetInfoList(current, pageSize, where, null);
        }

        /// <summary>
        /// 获取多个管理员信息
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pageSize">每页含有信息个数</param>
        /// <returns>多个管理员信息</returns>
        public List<Model.AUsers> GetInfoList(int current, int pageSize)
        {
            return GetInfoList(current, pageSize, null);
        }
    }
}
